{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "A8yPh4q8BpH2"
      },
      "source": [
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pinecone-io/examples/blob/master/learn/generation/langchain/handbook/01-langchain-prompt-templates.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/pinecone-io/examples/blob/master/learn/generation/langchain/handbook/01-langchain-prompt-templates.ipynb)\n",
        "\n",
        "# Prompt Engineering\n",
        "\n",
        "In this notebook we'll explore the fundamentals of prompt engineering. We'll start by installing library prerequisites."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "J5sxcgQpBpH2",
        "outputId": "927a1d12-c490-44be-9258-b31419746a71"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\u001b[?25l   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/65.3 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m65.3/65.3 kB\u001b[0m \u001b[31m3.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h"
          ]
        }
      ],
      "source": [
        "!pip install -qU \\\n",
        "  langchain==0.3.25 \\\n",
        "  langchain-openai==0.3.22"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9zdqD31pBpH3",
        "outputId": "4e91f0a8-8cf8-4d0b-e422-b9e873523092"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Enter your OpenAI API key: ··········\n"
          ]
        }
      ],
      "source": [
        "import os\n",
        "from getpass import getpass\n",
        "\n",
        "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\") \\\n",
        "    or getpass(\"Enter your OpenAI API key: \")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "vOoCmeDZBpH3"
      },
      "source": [
        "## Structure of a Prompt\n",
        "\n",
        "A prompt can consist of multiple components:\n",
        "\n",
        "* Instructions\n",
        "* External information or context\n",
        "* User input or query\n",
        "* Output indicator\n",
        "\n",
        "Not all prompts require all of these components, but often a good prompt will use two or more of them. Let's define what they all are more precisely.\n",
        "\n",
        "**Instructions** tell the model what to do, typically how it should use inputs and/or external information to produce the output we want.\n",
        "\n",
        "**External information or context** are additional information that we either manually insert into the prompt, retrieve via a vector database (long-term memory), or pull in through other means (API calls, calculations, etc).\n",
        "\n",
        "**User input or query** is typically a query directly input by the user of the system.\n",
        "\n",
        "**Output indicator** is the *beginning* of the generated text. For a model generating Python code we may put `import ` (as most Python scripts begin with a library `import`), or a chatbot may begin with `Chatbot: ` (assuming we format the chatbot script as lines of interchanging text between `User` and `Chatbot`).\n",
        "\n",
        "Each of these components should usually be placed the order we've described them. We start with instructions, provide context (if needed), then add the user input, and finally end with the output indicator."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "A-4Hr2K3BpH3"
      },
      "outputs": [],
      "source": [
        "prompt = \"\"\"Answer the question based on the context below. If the\n",
        "question cannot be answered using the information provided answer\n",
        "with \"I don't know\".\n",
        "\n",
        "Context: Large Language Models (LLMs) are the latest models used in NLP.\n",
        "Their superior performance over smaller models has made them incredibly\n",
        "useful for developers building NLP enabled applications. These models\n",
        "can be accessed via Hugging Face's `transformers` library, via OpenAI\n",
        "using the `openai` library, and via Cohere using the `cohere` library.\n",
        "\n",
        "Question: Which libraries and model providers offer LLMs?\n",
        "\n",
        "Answer: \"\"\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "unAseqe-BpH3"
      },
      "source": [
        "In this example we have:\n",
        "\n",
        "```\n",
        "Instructions\n",
        "\n",
        "Context\n",
        "\n",
        "Question (user input)\n",
        "\n",
        "Output indicator (\"Answer: \")\n",
        "```\n",
        "\n",
        "Let's try sending this to a GPT-3 model. We will use the LangChain library but you can also use the `openai` library directly. In both cases, you will need [an OpenAI API key](https://platform.openai.com/api-keys).\n",
        "\n",
        "We initialize a `gpt-4.1-mini` model like so:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "id": "qHP18qqsBpH3"
      },
      "outputs": [],
      "source": [
        "from langchain_openai import ChatOpenAI\n",
        "\n",
        "# initialize the models\n",
        "openai = ChatOpenAI(\n",
        "    model_name=\"gpt-4.1-mini\",\n",
        "    temperature=0.1\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "oNxIplxSBpH3"
      },
      "source": [
        "And make a generation from our prompt."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "nJXfuIzYBpH3",
        "outputId": "1c65ecee-3746-4da3-a350-043535a2aaf7"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Response: The libraries and model providers that offer LLMs are Hugging Face's `transformers` library, OpenAI using the `openai` library, and Cohere using the `cohere` library.\n"
          ]
        }
      ],
      "source": [
        "result = openai.invoke(prompt)\n",
        "print(f\"Response: {result.content}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4nkVAZ_HBpH3"
      },
      "source": [
        "We wouldn't typically know what the users prompt is beforehand. So rather than writing the full prompt, we create a `PromptTemplate` with a single input variable `query`."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "q9rLA8zMBpH3"
      },
      "outputs": [],
      "source": [
        "from langchain_core.prompts import PromptTemplate\n",
        "\n",
        "\n",
        "template = \"\"\"Answer the question based on the context below. If the\n",
        "question cannot be answered using the information provided answer\n",
        "with \"I don't know\".\n",
        "\n",
        "Context: Large Language Models (LLMs) are the latest models used in NLP.\n",
        "Their superior performance over smaller models has made them incredibly\n",
        "useful for developers building NLP enabled applications. These models\n",
        "can be accessed via Hugging Face's `transformers` library, via OpenAI\n",
        "using the `openai` library, and via Cohere using the `cohere` library.\n",
        "\n",
        "Question: {query}\n",
        "\n",
        "Answer: \"\"\"\n",
        "\n",
        "prompt_template = PromptTemplate(\n",
        "    input_variables=[\"query\"],\n",
        "    template=template\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-tEZCswhBpH4"
      },
      "source": [
        "Now we can insert the user's `query` to the prompt template via the `query` parameter."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "6cYuqS4pBpH4",
        "outputId": "21230f38-ed03-4af2-baa2-812406c81ea7"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Response: The libraries and model providers that offer LLMs are Hugging Face's `transformers` library, OpenAI using the `openai` library, and Cohere using the `cohere` library.\n"
          ]
        }
      ],
      "source": [
        "from langchain_core.output_parsers import StrOutputParser\n",
        "\n",
        "output_parser = StrOutputParser()\n",
        "chain = prompt_template | openai | output_parser\n",
        "\n",
        "response = chain.invoke({\"query\": \"Which libraries and model providers offer LLMs?\"})\n",
        "print(f\"Response: {response}\")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Br_OJ0j6BpH4"
      },
      "source": [
        "This is just a simple implementation, that we can easily replace with f-strings (like `f\"insert some custom text '{custom_text}' etc\"`). But using LangChain's `PromptTemplate` object we're able to formalize the process, add multiple parameters, and build the prompts in an object-oriented way.\n",
        "\n",
        "Yet, these are not the only benefits of using LangChains prompt tooling."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "HcnakinFBpH4"
      },
      "source": [
        "## Few Shot Prompt Templates"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qgBRmGYsBpH4"
      },
      "source": [
        "Another useful feature offered by LangChain is the `FewShotPromptTemplate` object. This is ideal for what we'd call *few-shot learning* using our prompts.\n",
        "\n",
        "To give some context, the primary sources of \"knowledge\" for LLMs are:\n",
        "\n",
        "* **Parametric knowledge** — the knowledge has been learned during model training and is stored within the model weights.\n",
        "\n",
        "* **Source knowledge** — the knowledge is provided within model input at inference time, i.e. via the prompt.\n",
        "\n",
        "The idea behind `FewShotPromptTemplate` is to provide few-shot training as **source knowledge**. To do this we add a few examples to our prompts that the model can read and then apply to our user's input."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "RWh3WyoWBpH4"
      },
      "source": [
        "## Few-shot Training\n",
        "\n",
        "Sometimes we might find that a model doesn't seem to get what we'd like it to do. We can see this in the following example:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 191
        },
        "id": "d81sqNkIBpH4",
        "outputId": "3f00e2b3-b953-422d-d6a0-4c8a35d34e42"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": [
              "```markdown\n",
              "# Frequently Asked Questions (FAQ)\n",
              "\n",
              "### What is your return policy?\n",
              "We accept returns within 30 days with receipt.\n",
              "\n",
              "### Do you ship internationally?\n",
              "Yes, we ship to over 50 countries.\n",
              "\n",
              "### How can I track my order?\n",
              "Use the tracking link in your confirmation email.\n",
              "```"
            ],
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "execution_count": 8,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from IPython.display import Markdown\n",
        "\n",
        "prompt = PromptTemplate(\n",
        "    template=(\n",
        "        \"Create an FAQ in Markdown based on the following questions and answers:\\n\"\n",
        "        \"Q1: What is your return policy?\\n\"\n",
        "        \"A1: We accept returns within 30 days with receipt.\\n\"\n",
        "        \"Q2: Do you ship internationally?\\n\"\n",
        "        \"A2: Yes, we ship to over 50 countries.\\n\"\n",
        "        \"Q3: How can I track my order?\\n\"\n",
        "        \"A3: Use the tracking link in your confirmation email.\"\n",
        "    ),\n",
        "    input_variables=[]  # No variables needed yet\n",
        ")\n",
        "\n",
        "chain = prompt | openai | output_parser\n",
        "response = chain.invoke({})  # Empty dict since no input variables\n",
        "Markdown(response)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "X6zTnGRqBpH4"
      },
      "source": [
        "The output results are okay. However, what if the Markdown headers aren't at the correct level (wrong mumber of hashes), and what if we want more interesting formatting?\n",
        "\n",
        "We could explain the desired formatting in painful detail in a large wordy paragraph, but that might be a bit fiddly and would likely lead to hallucinations.\n",
        "\n",
        "If a picture is worth a thousand words then examples are probably worth at least one hundred! We can give **examples** of what the outputs should look like - i.e. few-shot training."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 287
        },
        "id": "hBR3-XrqBpH4",
        "outputId": "006fc442-ce9c-4872-e1b0-8ad015b342b6"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": [
              "# Frequently Asked Questions\n",
              "\n",
              "## [1. What payment methods do you accept?](#1-what-payment-methods-do-you-accept)\n",
              "\n",
              "We accept **Visa**, **Mastercard**, **PayPal**, and **Apple Pay**.\n",
              "\n",
              "## [2. Can I change my shipping address after ordering?](#2-can-i-change-my-shipping-address-after-ordering)\n",
              "\n",
              "Only if your order hasn't shipped yet. Contact **support ASAP**.\n",
              "\n",
              "## [3. Do you offer gift wrapping?](#3-do-you-offer-gift-wrapping)\n",
              "\n",
              "Yes! You can select **gift wrapping** during checkout.\n",
              "\n",
              "---"
            ],
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "prompt_str = \"\"\"\n",
        "Create a structured Markdown FAQ with anchor links, headers, and formatting conventions for readability.\n",
        "\n",
        "**Example:**\n",
        "\n",
        "Example input:\n",
        "Q1: What is your return policy?\n",
        "A1: We accept returns within 30 days with the original receipt.\n",
        "Q2: Do you ship internationally?\n",
        "A2: Yes, we ship to over 50 countries worldwide.\n",
        "Q3: How can I track my order?\n",
        "A3: After your order is shipped, you'll receive a tracking link via email.\n",
        "\n",
        "Example Output:\n",
        "\n",
        "# Frequently Asked Questions\n",
        "\n",
        "## [1. What is your return policy?](#1-what-is-your-return-policy)\n",
        "\n",
        "We accept returns within **30 days** with the original **receipt**.\n",
        "\n",
        "## [2. Do you ship internationally?](#2-do-you-ship-internationally)\n",
        "\n",
        "Yes, we ship to over **50 countries** worldwide.\n",
        "\n",
        "## [3. How can I track my order?](#3-how-can-i-track-my-order)\n",
        "\n",
        "After your order is shipped, you'll receive a **tracking link** via email.\n",
        "\n",
        "---\n",
        "\n",
        "Now generate the FAQ section for this data:\n",
        "Q1: What payment methods do you accept?\n",
        "A1: We accept Visa, Mastercard, PayPal, and Apple Pay.\n",
        "Q2: Can I change my shipping address after ordering?\n",
        "A2: Only if your order hasn't shipped yet. Contact support ASAP.\n",
        "Q3: Do you offer gift wrapping?\n",
        "A3: Yes! You can select gift wrapping during checkout.\n",
        "\n",
        "\"\"\"\n",
        "\n",
        "# Create a PromptTemplate with no input variables (static prompt)\n",
        "prompt = PromptTemplate(template=prompt_str, input_variables=[])\n",
        "\n",
        "chain = prompt | openai | output_parser\n",
        "response = chain.invoke({})  # Empty dict since no input variables\n",
        "Markdown(response)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Lq3gfrrSBpH4"
      },
      "source": [
        "We now get a much better response and we did this via *few-shot learning* by adding a few examples via our source knowledge.\n",
        "\n",
        "Now, to implement this with LangChain's `FewShotPromptTemplate` we need to do this:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "id": "A9dKD8MoBpH4"
      },
      "outputs": [],
      "source": [
        "from langchain_core.prompts import FewShotPromptTemplate\n",
        "\n",
        "# Simplified examples with just 2 variables instead of 9\n",
        "examples = [\n",
        "    {\n",
        "        \"input\": (\n",
        "            \"Q1: What is your return policy?\\n\"\n",
        "            \"A1: We accept returns within 30 days with the original receipt.\"\n",
        "        ),\n",
        "        \"output\": (\n",
        "            \"## [1. What is your return policy?](#1-what-is-your-return-policy)\\n\\n\"\n",
        "            \"We accept returns within **30 days** with the original **receipt**.\"\n",
        "        )\n",
        "    },\n",
        "    {\n",
        "        \"input\": (\n",
        "            \"Q2: Do you ship internationally?\\n\"\n",
        "            \"A2: Yes, we ship to over 50 countries worldwide.\"\n",
        "        ),\n",
        "        \"output\": (\n",
        "            \"## [2. Do you ship internationally?](#2-do-you-ship-internationally)\\n\\n\"\n",
        "            \"Yes, we ship to over **50 countries** worldwide.\"\n",
        "        )\n",
        "    },\n",
        "    {\n",
        "        \"input\": (\n",
        "            \"Q3: How can I track my order?\\n\"\n",
        "            \"A3: After your order is shipped, you'll receive a tracking link via email.\"\n",
        "        ),\n",
        "        \"output\": (\n",
        "            \"## [3. How can I track my order?](#3-how-can-i-track-my-order)\\n\\n\"\n",
        "            \"After your order is shipped, you'll receive a **tracking link** via email.\"\n",
        "        )\n",
        "    }\n",
        "]\n",
        "\n",
        "# Much simpler example template with only 2 variables\n",
        "example_template = \"\"\"Example input:\n",
        "{input}\n",
        "\n",
        "Example Output:\n",
        "# Frequently Asked Questions\\n\\n\n",
        "{output}\n",
        "\"\"\"\n",
        "\n",
        "# Create prompt template for examples\n",
        "example_prompt = PromptTemplate(\n",
        "    input_variables=[\"input\", \"output\"],\n",
        "    template=example_template\n",
        ")\n",
        "\n",
        "# Instructions prefix\n",
        "prefix = \"\"\"Create a structured Markdown FAQ with anchor links, headers, and formatting conventions for readability.\n",
        "Make sure to bold key terms and important information in the answers.\n",
        "\n",
        "**Example:**\n",
        "\"\"\"\n",
        "\n",
        "# User input format and instructions\n",
        "suffix = \"\"\"\n",
        "Now generate the FAQ section for this data:\n",
        "{input}\n",
        "\"\"\"\n",
        "\n",
        "# Create the few-shot prompt template\n",
        "faq_few_shot = FewShotPromptTemplate(\n",
        "    examples=examples,\n",
        "    example_prompt=example_prompt,\n",
        "    prefix=prefix,\n",
        "    suffix=suffix,\n",
        "    input_variables=[\"input\"],\n",
        "    example_separator=\"\\n\"\n",
        ")\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "qMZl_g1CBpH4"
      },
      "source": [
        "Now let's see what prompt this creates when we feed in a user query..."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "7nt8RSsZBpH4",
        "outputId": "47188624-16ce-4b31-d1ba-cf41decee4a9"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Create a structured Markdown FAQ with anchor links, headers, and formatting conventions for readability.\n",
            "Make sure to bold key terms and important information in the answers.\n",
            "\n",
            "**Example:**\n",
            "\n",
            "Example input:\n",
            "Q1: What is your return policy?\n",
            "A1: We accept returns within 30 days with the original receipt.\n",
            "\n",
            "Example Output:\n",
            "# Frequently Asked Questions\n",
            "\n",
            "\n",
            "## [1. What is your return policy?](#1-what-is-your-return-policy)\n",
            "\n",
            "We accept returns within **30 days** with the original **receipt**.\n",
            "\n",
            "Example input:\n",
            "Q2: Do you ship internationally?\n",
            "A2: Yes, we ship to over 50 countries worldwide.\n",
            "\n",
            "Example Output:\n",
            "# Frequently Asked Questions\n",
            "\n",
            "\n",
            "## [2. Do you ship internationally?](#2-do-you-ship-internationally)\n",
            "\n",
            "Yes, we ship to over **50 countries** worldwide.\n",
            "\n",
            "Example input:\n",
            "Q3: How can I track my order?\n",
            "A3: After your order is shipped, you'll receive a tracking link via email.\n",
            "\n",
            "Example Output:\n",
            "# Frequently Asked Questions\n",
            "\n",
            "\n",
            "## [3. How can I track my order?](#3-how-can-i-track-my-order)\n",
            "\n",
            "After your order is shipped, you'll receive a **tracking link** via email.\n",
            "\n",
            "\n",
            "Now generate the FAQ section for this data:\n",
            "Q1: What payment methods do you accept?\n",
            "A1: We accept Visa, Mastercard, PayPal, and Apple Pay.\n",
            "Q2: Can I change my shipping address after ordering?\n",
            "A2: Only if your order hasn't shipped yet. Contact support ASAP.\n",
            "Q3: Do you offer gift wrapping?\n",
            "A3: Yes! You can select gift wrapping during checkout.\n",
            "\n"
          ]
        }
      ],
      "source": [
        "# See what the prompt looks like before sending to LLM\n",
        "user_input = \"\"\"Q1: What payment methods do you accept?\n",
        "A1: We accept Visa, Mastercard, PayPal, and Apple Pay.\n",
        "Q2: Can I change my shipping address after ordering?\n",
        "A2: Only if your order hasn't shipped yet. Contact support ASAP.\n",
        "Q3: Do you offer gift wrapping?\n",
        "A3: Yes! You can select gift wrapping during checkout.\"\"\"\n",
        "\n",
        "formatted_prompt = faq_few_shot.format(input=user_input)\n",
        "print(formatted_prompt)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "aVeztY56BpH4"
      },
      "source": [
        "We can use an LCEL chain to generate responses"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 277
        },
        "id": "KMESOPq5BpH4",
        "outputId": "85b9de9a-d6b1-402e-b330-212f7b13c796"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": [
              "# Frequently Asked Questions\n",
              "\n",
              "\n",
              "## [1. What payment methods do you accept?](#1-what-payment-methods-do-you-accept)\n",
              "\n",
              "We accept **Visa**, **Mastercard**, **PayPal**, and **Apple Pay**.\n",
              "\n",
              "\n",
              "## [2. Can I change my shipping address after ordering?](#2-can-i-change-my-shipping-address-after-ordering)\n",
              "\n",
              "Only if your order **hasn't shipped yet**. Contact **support ASAP**.\n",
              "\n",
              "\n",
              "## [3. Do you offer gift wrapping?](#3-do-you-offer-gift-wrapping)\n",
              "\n",
              "Yes! You can select **gift wrapping** during **checkout**."
            ],
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "execution_count": 12,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "chain = faq_few_shot | openai | output_parser\n",
        "\n",
        "# Execute with LCEL chain\n",
        "response = chain.invoke({\"input\": user_input})\n",
        "\n",
        "# Display the formatted FAQ (response is already a clean string)\n",
        "Markdown(response)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "clT3GSn9BpH4"
      },
      "source": [
        "Again, another good response.\n",
        "\n",
        "However, this does seem somewhat convoluted. Why go through all of the above with `FewShotPromptTemplate`, the `examples` dictionary, etc — when we can do the same with a single f-string.\n",
        "\n",
        "This approach is more robust and contains some nice features. One of those is the ability to include or exclude examples based on the length of our input FAQ data.\n",
        "\n",
        "This is actually very important because the max length of our prompt and generation output is limited. This limitation is the *max context window*, and is simply the length of our prompt + length of our generation (which we define via `max_tokens`).\n",
        "\n",
        "So we must try to maximize the number of example FAQs we give to the model as few-shot learning examples, while ensuring we don't exceed the maximum context window or increase processing times excessively.\n",
        "\n",
        "Let's see how the dynamic inclusion/exclusion of examples works. First we need lots of FAQ examples:\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {
        "id": "ikOJpsC-BpH5"
      },
      "outputs": [],
      "source": [
        "# note we're adding more FAQ examples to each individual example here\n",
        "# (how much you add isn't necessarily important - just that you're aligning\n",
        "# examples as closely as possible to your use-case)\n",
        "examples = [\n",
        "    {\n",
        "        \"input\": \"\"\"Q1: What is your return policy?\n",
        "A1: We accept returns within 30 days with the original receipt.\n",
        "Q2: Do you ship internationally?\n",
        "A2: Yes, we ship to over 50 countries worldwide.\n",
        "Q3: How can I track my order?\n",
        "A3: After your order is shipped, you'll receive a tracking link via email.\"\"\",\n",
        "        \"output\": \"\"\"# Frequently Asked Questions\n",
        "\n",
        "## [1. What is your return policy?](#1-what-is-your-return-policy)\n",
        "\n",
        "We accept returns within **30 days** with the original **receipt**.\n",
        "\n",
        "## [2. Do you ship internationally?](#2-do-you-ship-internationally)\n",
        "\n",
        "Yes, we ship to over **50 countries** worldwide.\n",
        "\n",
        "## [3. How can I track my order?](#3-how-can-i-track-my-order)\n",
        "\n",
        "After your order is shipped, you'll receive a **tracking link** via email.\n",
        "\n",
        "---\"\"\"\n",
        "    },\n",
        "    {\n",
        "        \"input\": \"\"\"Q1: What payment methods do you accept?\n",
        "A1: We accept Visa, Mastercard, PayPal, and Apple Pay.\n",
        "Q2: Can I change my shipping address after ordering?\n",
        "A2: Only if your order hasn't shipped yet. Contact support ASAP.\n",
        "Q3: Do you offer gift wrapping?\n",
        "A3: Yes! You can select gift wrapping during checkout.\"\"\",\n",
        "        \"output\": \"\"\"# Frequently Asked Questions\n",
        "\n",
        "## [1. What payment methods do you accept?](#1-what-payment-methods-do-you-accept)\n",
        "\n",
        "We accept **Visa**, **Mastercard**, **PayPal**, and **Apple Pay**.\n",
        "\n",
        "## [2. Can I change my shipping address after ordering?](#2-can-i-change-my-shipping-address-after-ordering)\n",
        "\n",
        "Only if your order hasn't shipped yet. Contact support **ASAP**.\n",
        "\n",
        "## [3. Do you offer gift wrapping?](#3-do-you-offer-gift-wrapping)\n",
        "\n",
        "Yes! You can select **gift wrapping** during checkout.\n",
        "\n",
        "---\"\"\"\n",
        "    },\n",
        "    {\n",
        "        \"input\": \"\"\"Q1: What are your store hours?\n",
        "A1: We're open Monday through Friday from 9am to 9pm, and weekends from 10am to 7pm.\n",
        "Q2: Do you offer price matching?\n",
        "A2: Yes, we'll match any price from authorized retailers for identical products.\n",
        "Q3: What is your warranty policy?\n",
        "A3: All electronics come with a standard 1-year manufacturer warranty.\"\"\",\n",
        "        \"output\": \"\"\"# Frequently Asked Questions\n",
        "\n",
        "## [1. What are your store hours?](#1-what-are-your-store-hours)\n",
        "\n",
        "We're open **Monday through Friday** from **9am to 9pm**, and **weekends** from **10am to 7pm**.\n",
        "\n",
        "## [2. Do you offer price matching?](#2-do-you-offer-price-matching)\n",
        "\n",
        "Yes, we'll match any price from **authorized retailers** for **identical products**.\n",
        "\n",
        "## [3. What is your warranty policy?](#3-what-is-your-warranty-policy)\n",
        "\n",
        "All electronics come with a standard **1-year manufacturer warranty**.\n",
        "\n",
        "---\"\"\"\n",
        "    },\n",
        "    {\n",
        "        \"input\": \"\"\"Q1: How do I contact customer service?\n",
        "A1: You can reach us at support@example.com or call 555-123-4567.\n",
        "Q2: Do you offer same-day delivery?\n",
        "A2: Yes, for orders placed before 2pm in selected metro areas.\n",
        "Q3: How do I cancel an order?\n",
        "A3: Log into your account and cancel within 1 hour of placing the order.\"\"\",\n",
        "        \"output\": \"\"\"# Frequently Asked Questions\n",
        "\n",
        "## [1. How do I contact customer service?](#1-how-do-i-contact-customer-service)\n",
        "\n",
        "You can reach us at **support@example.com** or call **555-123-4567**.\n",
        "\n",
        "## [2. Do you offer same-day delivery?](#2-do-you-offer-same-day-delivery)\n",
        "\n",
        "Yes, for orders placed before **2pm** in selected **metro areas**.\n",
        "\n",
        "## [3. How do I cancel an order?](#3-how-do-i-cancel-an-order)\n",
        "\n",
        "Log into your account and cancel within **1 hour** of placing the order.\n",
        "\n",
        "---\"\"\"\n",
        "    },\n",
        "    {\n",
        "        \"input\": \"\"\"Q1: Do you offer student discounts?\n",
        "A1: Yes, students with valid ID receive 15% off all purchases.\n",
        "Q2: What is your privacy policy?\n",
        "A2: We never share your personal information with third parties without consent.\n",
        "Q3: Are your products environmentally friendly?\n",
        "A3: We use recyclable packaging and offer carbon-neutral shipping options.\"\"\",\n",
        "        \"output\": \"\"\"# Frequently Asked Questions\n",
        "\n",
        "## [1. Do you offer student discounts?](#1-do-you-offer-student-discounts)\n",
        "\n",
        "Yes, students with valid ID receive **15% off** all purchases.\n",
        "\n",
        "## [2. What is your privacy policy?](#2-what-is-your-privacy-policy)\n",
        "\n",
        "We **never share** your personal information with third parties without consent.\n",
        "\n",
        "## [3. Are your products environmentally friendly?](#3-are-your-products-environmentally-friendly)\n",
        "\n",
        "We use **recyclable packaging** and offer **carbon-neutral shipping** options.\n",
        "\n",
        "---\"\"\"\n",
        "    },\n",
        "    {\n",
        "        \"input\": \"\"\"Q1: How do I apply for a refund?\n",
        "A1: Submit your request through our customer portal with your order number and reason.\n",
        "Q2: Do you have a loyalty program?\n",
        "A2: Yes! Earn 1 point for every dollar spent and redeem for discounts.\n",
        "Q3: What are the system requirements?\n",
        "A3: Our software requires Windows 10/11 or macOS 10.15+, 8GB RAM, and 2GB storage.\"\"\",\n",
        "        \"output\": \"\"\"# Frequently Asked Questions\n",
        "\n",
        "## [1. How do I apply for a refund?](#1-how-do-i-apply-for-a-refund)\n",
        "\n",
        "Submit your request through our **customer portal** with your **order number** and reason.\n",
        "\n",
        "## [2. Do you have a loyalty program?](#2-do-you-have-a-loyalty-program)\n",
        "\n",
        "Yes! Earn **1 point** for every dollar spent and redeem for **discounts**.\n",
        "\n",
        "## [3. What are the system requirements?](#3-what-are-the-system-requirements)\n",
        "\n",
        "Our software requires **Windows 10/11** or **macOS 10.15+**, **8GB RAM**, and **2GB storage**.\n",
        "\n",
        "---\"\"\"\n",
        "    },\n",
        "    {\n",
        "        \"input\": \"\"\"Q1: How long does shipping take?\n",
        "A1: Standard shipping takes 3-5 business days, and express shipping takes 1-2 business days.\n",
        "Q2: Do you have physical stores?\n",
        "A2: Yes, we have 12 locations across North America. Find the nearest one on our website.\n",
        "Q3: How do I reset my password?\n",
        "A3: Click 'Forgot Password' on the login page and follow the email instructions.\"\"\",\n",
        "        \"output\": \"\"\"# Frequently Asked Questions\n",
        "\n",
        "## [1. How long does shipping take?](#1-how-long-does-shipping-take)\n",
        "\n",
        "Standard shipping takes **3-5 business days**, and express shipping takes **1-2 business days**.\n",
        "\n",
        "## [2. Do you have physical stores?](#2-do-you-have-physical-stores)\n",
        "\n",
        "Yes, we have **12 locations** across North America. Find the nearest one on our **website**.\n",
        "\n",
        "## [3. How do I reset my password?](#3-how-do-i-reset-my-password)\n",
        "\n",
        "Click **'Forgot Password'** on the login page and follow the email instructions.\n",
        "\n",
        "---\"\"\"\n",
        "    },\n",
        "    {\n",
        "        \"input\": \"\"\"Q1: What's your wholesale policy?\n",
        "A1: For orders over $500, contact our wholesale department for special pricing.\n",
        "Q2: How can I become a vendor?\n",
        "A2: Fill out the vendor application form on our Partners page for consideration.\n",
        "Q3: Do you offer installation services?\n",
        "A3: Yes, professional installation is available for an additional fee in most areas.\"\"\",\n",
        "        \"output\": \"\"\"# Frequently Asked Questions\n",
        "\n",
        "## [1. What's your wholesale policy?](#1-whats-your-wholesale-policy)\n",
        "\n",
        "For orders over **$500**, contact our **wholesale department** for special pricing.\n",
        "\n",
        "## [2. How can I become a vendor?](#2-how-can-i-become-a-vendor)\n",
        "\n",
        "Fill out the **vendor application form** on our **Partners page** for consideration.\n",
        "\n",
        "## [3. Do you offer installation services?](#3-do-you-offer-installation-services)\n",
        "\n",
        "Yes, **professional installation** is available for an additional fee in **most areas**.\n",
        "\n",
        "---\"\"\"\n",
        "    }\n",
        "]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "IYNKfuHYBpH5"
      },
      "source": [
        "Then rather than using the `examples` list of dictionaries directly we use a `LengthBasedExampleSelector` like so:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "metadata": {
        "id": "tmq81HHFBpH5"
      },
      "outputs": [],
      "source": [
        "from langchain.prompts.example_selector import LengthBasedExampleSelector\n",
        "\n",
        "example_selector = LengthBasedExampleSelector(\n",
        "    examples=examples,\n",
        "    example_prompt=example_prompt,\n",
        "    max_length=700  # this sets the max length that examples should be\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "UtQuI_F7BpH5"
      },
      "source": [
        "Note that the `max_length` is measured as [a split of words between newlines and spaces](https://github.com/langchain-ai/langchain/blob/3c3320ae30151b20b4f59df7079b061329712080/libs/core/langchain_core/example_selectors/length_based.py#L13), determined by:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "IsymgITwBpH5",
        "outputId": "4fb85788-de57-4a4e-9052-372df9f55239"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "['There', 'are', 'a', 'total', 'of', '8', 'words', 'here.', 'Plus', '6', 'here,', 'totaling', '14', 'words.'] 14\n"
          ]
        }
      ],
      "source": [
        "import re\n",
        "\n",
        "some_text = \"There are a total of 8 words here.\\nPlus 6 here, totaling 14 words.\"\n",
        "\n",
        "words = re.split('[\\n ]', some_text)\n",
        "print(words, len(words))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "2EPHpeOYBpH5"
      },
      "source": [
        "Then we use the selector to initialize a `dynamic_prompt_template`."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "metadata": {
        "id": "8y7oAQMHBpH5"
      },
      "outputs": [],
      "source": [
        "# Then create the dynamic prompt template\n",
        "dynamic_faq_prompt = FewShotPromptTemplate(\n",
        "    example_selector=example_selector,  # use example_selector instead of examples\n",
        "    example_prompt=example_prompt,\n",
        "    prefix=prefix,\n",
        "    suffix=suffix,\n",
        "    input_variables=[\"input\"],  # simplified to just one variable\n",
        "    example_separator=\"\\n\"\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "CsPia7Q0BpH5"
      },
      "source": [
        "We can see that the number of included prompts will vary based on the length of our query. First let's start with a relatively short user input."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "metadata": {
        "id": "ZNIeVgDvBpH5"
      },
      "outputs": [],
      "source": [
        "# Using simplified input format\n",
        "user_input = \"\"\"Q1: What payment methods do you accept?\n",
        "A1: We accept Visa, Mastercard, PayPal, and Apple Pay.\n",
        "Q2: Can I change my shipping address after ordering?\n",
        "A2: Only if your order hasn't shipped yet. Contact support ASAP.\n",
        "Q3: Do you offer gift wrapping?\n",
        "A3: Yes! You can select gift wrapping during checkout.\"\"\"\n",
        "\n",
        "formatted_prompt = dynamic_faq_prompt.format(input=user_input)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "AfMCvmDpBpH5"
      },
      "source": [
        "As you can see, the dynamically formatted prompt includes an number of examples."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "rU3s3jrdBpH5",
        "outputId": "f04f8339-f009-415b-cff9-c8c6131d0359"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Create a structured Markdown FAQ with anchor links, headers, and formatting conventions for readability.\n",
            "Make sure to bold key terms and important information in the answers.\n",
            "\n",
            "**Example:**\n",
            "\n",
            "Example input:\n",
            "Q1: What is your return policy?\n",
            "A1: We accept returns within 30 days with the original receipt.\n",
            "Q2: Do you ship internationally?\n",
            "A2: Yes, we ship to over 50 countries worldwide.\n",
            "Q3: How can I track my order?\n",
            "A3: After your order is shipped, you'll receive a tracking link via email.\n",
            "\n",
            "Example Output:\n",
            "# Frequently Asked Questions\n",
            "\n",
            "\n",
            "# Frequently Asked Questions\n",
            "\n",
            "## [1. What is your return policy?](#1-what-is-your-return-policy)\n",
            "\n",
            "We accept returns within **30 days** with the original **receipt**.\n",
            "\n",
            "## [2. Do you ship internationally?](#2-do-you-ship-internationally)\n",
            "\n",
            "Yes, we ship to over **50 countries** worldwide.\n",
            "\n",
            "## [3. How can I track my order?](#3-how-can-i-track-my-order)\n",
            "\n",
            "After your order is shipped, you'll receive a **tracking link** via email.\n",
            "\n",
            "---\n",
            "\n",
            "Example input:\n",
            "Q1: What payment methods do you accept?\n",
            "A1: We accept Visa, Mastercard, PayPal, and Apple Pay.\n",
            "Q2: Can I change my shipping address after ordering?\n",
            "A2: Only if your order hasn't shipped yet. Contact support ASAP.\n",
            "Q3: Do you offer gift wrapping?\n",
            "A3: Yes! You can select gift wrapping during checkout.\n",
            "\n",
            "Example Output:\n",
            "# Frequently Asked Questions\n",
            "\n",
            "\n",
            "# Frequently Asked Questions\n",
            "\n",
            "## [1. What payment methods do you accept?](#1-what-payment-methods-do-you-accept)\n",
            "\n",
            "We accept **Visa**, **Mastercard**, **PayPal**, and **Apple Pay**.\n",
            "\n",
            "## [2. Can I change my shipping address after ordering?](#2-can-i-change-my-shipping-address-after-ordering)\n",
            "\n",
            "Only if your order hasn't shipped yet. Contact support **ASAP**.\n",
            "\n",
            "## [3. Do you offer gift wrapping?](#3-do-you-offer-gift-wrapping)\n",
            "\n",
            "Yes! You can select **gift wrapping** during checkout.\n",
            "\n",
            "---\n",
            "\n",
            "Example input:\n",
            "Q1: What are your store hours?\n",
            "A1: We're open Monday through Friday from 9am to 9pm, and weekends from 10am to 7pm.\n",
            "Q2: Do you offer price matching?\n",
            "A2: Yes, we'll match any price from authorized retailers for identical products.\n",
            "Q3: What is your warranty policy?\n",
            "A3: All electronics come with a standard 1-year manufacturer warranty.\n",
            "\n",
            "Example Output:\n",
            "# Frequently Asked Questions\n",
            "\n",
            "\n",
            "# Frequently Asked Questions\n",
            "\n",
            "## [1. What are your store hours?](#1-what-are-your-store-hours)\n",
            "\n",
            "We're open **Monday through Friday** from **9am to 9pm**, and **weekends** from **10am to 7pm**.\n",
            "\n",
            "## [2. Do you offer price matching?](#2-do-you-offer-price-matching)\n",
            "\n",
            "Yes, we'll match any price from **authorized retailers** for **identical products**.\n",
            "\n",
            "## [3. What is your warranty policy?](#3-what-is-your-warranty-policy)\n",
            "\n",
            "All electronics come with a standard **1-year manufacturer warranty**.\n",
            "\n",
            "---\n",
            "\n",
            "Example input:\n",
            "Q1: How do I contact customer service?\n",
            "A1: You can reach us at support@example.com or call 555-123-4567.\n",
            "Q2: Do you offer same-day delivery?\n",
            "A2: Yes, for orders placed before 2pm in selected metro areas.\n",
            "Q3: How do I cancel an order?\n",
            "A3: Log into your account and cancel within 1 hour of placing the order.\n",
            "\n",
            "Example Output:\n",
            "# Frequently Asked Questions\n",
            "\n",
            "\n",
            "# Frequently Asked Questions\n",
            "\n",
            "## [1. How do I contact customer service?](#1-how-do-i-contact-customer-service)\n",
            "\n",
            "You can reach us at **support@example.com** or call **555-123-4567**.\n",
            "\n",
            "## [2. Do you offer same-day delivery?](#2-do-you-offer-same-day-delivery)\n",
            "\n",
            "Yes, for orders placed before **2pm** in selected **metro areas**.\n",
            "\n",
            "## [3. How do I cancel an order?](#3-how-do-i-cancel-an-order)\n",
            "\n",
            "Log into your account and cancel within **1 hour** of placing the order.\n",
            "\n",
            "---\n",
            "\n",
            "Example input:\n",
            "Q1: Do you offer student discounts?\n",
            "A1: Yes, students with valid ID receive 15% off all purchases.\n",
            "Q2: What is your privacy policy?\n",
            "A2: We never share your personal information with third parties without consent.\n",
            "Q3: Are your products environmentally friendly?\n",
            "A3: We use recyclable packaging and offer carbon-neutral shipping options.\n",
            "\n",
            "Example Output:\n",
            "# Frequently Asked Questions\n",
            "\n",
            "\n",
            "# Frequently Asked Questions\n",
            "\n",
            "## [1. Do you offer student discounts?](#1-do-you-offer-student-discounts)\n",
            "\n",
            "Yes, students with valid ID receive **15% off** all purchases.\n",
            "\n",
            "## [2. What is your privacy policy?](#2-what-is-your-privacy-policy)\n",
            "\n",
            "We **never share** your personal information with third parties without consent.\n",
            "\n",
            "## [3. Are your products environmentally friendly?](#3-are-your-products-environmentally-friendly)\n",
            "\n",
            "We use **recyclable packaging** and offer **carbon-neutral shipping** options.\n",
            "\n",
            "---\n",
            "\n",
            "\n",
            "Now generate the FAQ section for this data:\n",
            "Q1: What payment methods do you accept?\n",
            "A1: We accept Visa, Mastercard, PayPal, and Apple Pay.\n",
            "Q2: Can I change my shipping address after ordering?\n",
            "A2: Only if your order hasn't shipped yet. Contact support ASAP.\n",
            "Q3: Do you offer gift wrapping?\n",
            "A3: Yes! You can select gift wrapping during checkout.\n",
            "\n"
          ]
        }
      ],
      "source": [
        "print(formatted_prompt)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "aWq3yyFXBpH9"
      },
      "source": [
        "Here is the response we get when we use this prompt with the LLM."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 287
        },
        "id": "e7PAFbC_BpH9",
        "outputId": "741223fd-5b7c-450b-afd9-f0bdaa5c68d2"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": [
              "# Frequently Asked Questions\n",
              "\n",
              "\n",
              "## [1. What payment methods do you accept?](#1-what-payment-methods-do-you-accept)\n",
              "\n",
              "We accept **Visa**, **Mastercard**, **PayPal**, and **Apple Pay**.\n",
              "\n",
              "## [2. Can I change my shipping address after ordering?](#2-can-i-change-my-shipping-address-after-ordering)\n",
              "\n",
              "Only if your order hasn't shipped yet. Contact support **ASAP**.\n",
              "\n",
              "## [3. Do you offer gift wrapping?](#3-do-you-offer-gift-wrapping)\n",
              "\n",
              "Yes! You can select **gift wrapping** during checkout.\n",
              "\n",
              "---"
            ],
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "execution_count": 19,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "dynamic_chain = dynamic_faq_prompt | openai | output_parser\n",
        "\n",
        "# Execute with LCEL chain\n",
        "response = dynamic_chain.invoke({\"input\": user_input})\n",
        "Markdown(response)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "iC_uFdnGBpH9"
      },
      "source": [
        "However, what if the user inputs are much longer, using more off the availabel context window?"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 20,
      "metadata": {
        "id": "UDw5SC7WBpH9"
      },
      "outputs": [],
      "source": [
        "# Using simplified input format with very long questions and answers\n",
        "user_input = \"\"\"Q1: What are all the different payment methods that you accept for online purchases, including credit cards, digital wallets, bank transfers, and any special financing options that might be available for customers?\n",
        "A1: We accept a comprehensive range of payment methods to accommodate all our customers' preferences and needs. For credit cards, we accept Visa, Mastercard, American Express, and Discover. We also support digital wallet payments through PayPal, Apple Pay, Google Pay, Samsung Pay, and Amazon Pay. Additionally, we offer bank transfer options including ACH transfers, wire transfers, and direct debit for customers who prefer traditional banking methods. For larger purchases, we provide financing options through Affirm, Klarna, and our own in-house financing program with flexible payment plans ranging from 6 to 36 months. We also accept cryptocurrency payments including Bitcoin, Ethereum, and several other major cryptocurrencies for tech-savvy customers.\n",
        "Q2: Is it possible for me to modify or completely change my shipping address after I have already placed and confirmed my order, and if so, what are the specific conditions, timeframes, and procedures that I need to follow?\n",
        "A2: Yes, it is possible to modify your shipping address, but this depends entirely on the current status of your order in our fulfillment process. If your order has not yet been processed by our warehouse team and is still in 'pending' or 'confirmed' status, you can easily change the shipping address by logging into your account and accessing the order management section. However, once your order enters the 'processing' phase and our warehouse team begins preparing your items for shipment, address changes become much more complicated and may not be possible. If your order has already shipped, unfortunately we cannot redirect the package to a different address, but you can contact the shipping carrier directly to arrange for package interception or redirection services, though additional fees may apply. For the best chance of successful address modification, we strongly recommend contacting our customer support team immediately at support@company.com or calling our toll-free number.\n",
        "Q3: Do you provide gift wrapping services for the items that I purchase, and if you do, what are the different options available, what are the costs involved, and can I include personalized messages or special requests?\n",
        "A3: Absolutely! We offer comprehensive gift wrapping services to make your purchases extra special for any occasion. We have several gift wrapping options available: our standard gift wrap features elegant wrapping paper in various colors and patterns with matching ribbon and a bow for an additional $4.99 per item. Our premium gift wrap option includes luxury wrapping paper, silk ribbon, and decorative embellishments for $9.99 per item. For special occasions, we offer themed wrapping for holidays, birthdays, weddings, and baby showers at $7.99 per item. You can also add personalized gift messages up to 250 characters at no additional cost, and we'll include them on beautiful greeting cards. For an extra $2.99, you can upload custom messages or even photos to be printed on special cards. All gift-wrapped items are carefully packaged to ensure they arrive in perfect condition, and we offer discrete packaging options if you're sending gifts directly to recipients.\"\"\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "kQrcE2_OBpH9"
      },
      "source": [
        "Noice that the number of examples used is reduced, as more of the context is taken up by the user input, and less remains for examples."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 21,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "sfIc2500BpH9",
        "outputId": "6332d684-05b6-41b7-964e-224c6c70d93b"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Create a structured Markdown FAQ with anchor links, headers, and formatting conventions for readability.\n",
            "Make sure to bold key terms and important information in the answers.\n",
            "\n",
            "**Example:**\n",
            "\n",
            "Example input:\n",
            "Q1: What is your return policy?\n",
            "A1: We accept returns within 30 days with the original receipt.\n",
            "Q2: Do you ship internationally?\n",
            "A2: Yes, we ship to over 50 countries worldwide.\n",
            "Q3: How can I track my order?\n",
            "A3: After your order is shipped, you'll receive a tracking link via email.\n",
            "\n",
            "Example Output:\n",
            "# Frequently Asked Questions\n",
            "\n",
            "\n",
            "# Frequently Asked Questions\n",
            "\n",
            "## [1. What is your return policy?](#1-what-is-your-return-policy)\n",
            "\n",
            "We accept returns within **30 days** with the original **receipt**.\n",
            "\n",
            "## [2. Do you ship internationally?](#2-do-you-ship-internationally)\n",
            "\n",
            "Yes, we ship to over **50 countries** worldwide.\n",
            "\n",
            "## [3. How can I track my order?](#3-how-can-i-track-my-order)\n",
            "\n",
            "After your order is shipped, you'll receive a **tracking link** via email.\n",
            "\n",
            "---\n",
            "\n",
            "\n",
            "Now generate the FAQ section for this data:\n",
            "Q1: What are all the different payment methods that you accept for online purchases, including credit cards, digital wallets, bank transfers, and any special financing options that might be available for customers?\n",
            "A1: We accept a comprehensive range of payment methods to accommodate all our customers' preferences and needs. For credit cards, we accept Visa, Mastercard, American Express, and Discover. We also support digital wallet payments through PayPal, Apple Pay, Google Pay, Samsung Pay, and Amazon Pay. Additionally, we offer bank transfer options including ACH transfers, wire transfers, and direct debit for customers who prefer traditional banking methods. For larger purchases, we provide financing options through Affirm, Klarna, and our own in-house financing program with flexible payment plans ranging from 6 to 36 months. We also accept cryptocurrency payments including Bitcoin, Ethereum, and several other major cryptocurrencies for tech-savvy customers.\n",
            "Q2: Is it possible for me to modify or completely change my shipping address after I have already placed and confirmed my order, and if so, what are the specific conditions, timeframes, and procedures that I need to follow?\n",
            "A2: Yes, it is possible to modify your shipping address, but this depends entirely on the current status of your order in our fulfillment process. If your order has not yet been processed by our warehouse team and is still in 'pending' or 'confirmed' status, you can easily change the shipping address by logging into your account and accessing the order management section. However, once your order enters the 'processing' phase and our warehouse team begins preparing your items for shipment, address changes become much more complicated and may not be possible. If your order has already shipped, unfortunately we cannot redirect the package to a different address, but you can contact the shipping carrier directly to arrange for package interception or redirection services, though additional fees may apply. For the best chance of successful address modification, we strongly recommend contacting our customer support team immediately at support@company.com or calling our toll-free number.\n",
            "Q3: Do you provide gift wrapping services for the items that I purchase, and if you do, what are the different options available, what are the costs involved, and can I include personalized messages or special requests?\n",
            "A3: Absolutely! We offer comprehensive gift wrapping services to make your purchases extra special for any occasion. We have several gift wrapping options available: our standard gift wrap features elegant wrapping paper in various colors and patterns with matching ribbon and a bow for an additional $4.99 per item. Our premium gift wrap option includes luxury wrapping paper, silk ribbon, and decorative embellishments for $9.99 per item. For special occasions, we offer themed wrapping for holidays, birthdays, weddings, and baby showers at $7.99 per item. You can also add personalized gift messages up to 250 characters at no additional cost, and we'll include them on beautiful greeting cards. For an extra $2.99, you can upload custom messages or even photos to be printed on special cards. All gift-wrapped items are carefully packaged to ensure they arrive in perfect condition, and we offer discrete packaging options if you're sending gifts directly to recipients.\n",
            "\n"
          ]
        }
      ],
      "source": [
        "formatted_prompt = dynamic_faq_prompt.format(input=user_input)\n",
        "print(formatted_prompt)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "U_8AmNjNBpH9"
      },
      "source": [
        "The above shows that the `LengthBasedExampleSelector` class dynamically selects the examples without cutting any off halfway through. Now let's see what response this dynamically generated prompt gives."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 22,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 911
        },
        "id": "NH5yDUnlBpH9",
        "outputId": "fd04cda7-87d2-4f08-9f60-1c8c1548e60b"
      },
      "outputs": [
        {
          "data": {
            "text/markdown": [
              "# Frequently Asked Questions\n",
              "\n",
              "## [1. What are all the different payment methods that you accept for online purchases, including credit cards, digital wallets, bank transfers, and any special financing options that might be available for customers?](#1-what-are-all-the-different-payment-methods-that-you-accept-for-online-purchases-including-credit-cards-digital-wallets-bank-transfers-and-any-special-financing-options-that-might-be-available-for-customers)\n",
              "\n",
              "We accept a comprehensive range of **payment methods** to accommodate all our customers' preferences and needs:\n",
              "\n",
              "- **Credit Cards:** Visa, Mastercard, American Express, and Discover  \n",
              "- **Digital Wallets:** PayPal, Apple Pay, Google Pay, Samsung Pay, and Amazon Pay  \n",
              "- **Bank Transfers:** ACH transfers, wire transfers, and direct debit  \n",
              "- **Financing Options:** Affirm, Klarna, and our in-house financing program with flexible payment plans ranging from **6 to 36 months**  \n",
              "- **Cryptocurrency:** Bitcoin, Ethereum, and several other major cryptocurrencies  \n",
              "\n",
              "This variety ensures a convenient and flexible checkout experience for everyone.\n",
              "\n",
              "---\n",
              "\n",
              "## [2. Is it possible for me to modify or completely change my shipping address after I have already placed and confirmed my order, and if so, what are the specific conditions, timeframes, and procedures that I need to follow?](#2-is-it-possible-for-me-to-modify-or-completely-change-my-shipping-address-after-i-have-already-placed-and-confirmed-my-order-and-if-so-what-are-the-specific-conditions-timeframes-and-procedures-that-i-need-to-follow)\n",
              "\n",
              "Yes, you **can modify your shipping address**, but this depends on your order's current status:\n",
              "\n",
              "- If your order is still in **'pending'** or **'confirmed'** status (not yet processed by our warehouse), you can change the address by logging into your account and updating it in the **order management section**.  \n",
              "- Once your order enters the **'processing'** phase, address changes become difficult and may not be possible.  \n",
              "- If your order has **already shipped**, we cannot redirect the package. However, you may contact the **shipping carrier directly** to arrange interception or redirection (additional fees may apply).  \n",
              "\n",
              "For the best chance of successful modification, please contact our **customer support immediately** at:\n",
              "\n",
              "- Email: **support@company.com**  \n",
              "- Toll-free phone number: *(please refer to your order confirmation or website)*  \n",
              "\n",
              "---\n",
              "\n",
              "## [3. Do you provide gift wrapping services for the items that I purchase, and if you do, what are the different options available, what are the costs involved, and can I include personalized messages or special requests?](#3-do-you-provide-gift-wrapping-services-for-the-items-that-i-purchase-and-if-you-do-what-are-the-different-options-available-what-are-the-costs-involved-and-can-i-include-personalized-messages-or-special-requests)\n",
              "\n",
              "Absolutely! We offer **comprehensive gift wrapping services** with several options:\n",
              "\n",
              "- **Standard Gift Wrap:** Elegant wrapping paper in various colors and patterns with matching ribbon and bow — **$4.99 per item**  \n",
              "- **Premium Gift Wrap:** Luxury wrapping paper, silk ribbon, and decorative embellishments — **$9.99 per item**  \n",
              "- **Themed Wrapping:** For holidays, birthdays, weddings, and baby showers — **$7.99 per item**  \n",
              "\n",
              "**Personalization options:**\n",
              "\n",
              "- Add a **personalized gift message** (up to 250 characters) at **no additional cost**, included on beautiful greeting cards  \n",
              "- Upload **custom messages or photos** for printing on special cards — **$2.99 extra**  \n",
              "\n",
              "All gift-wrapped items are carefully packaged to ensure they arrive in **perfect condition**, and we offer **discrete packaging** if sending gifts directly to recipients.\n",
              "\n",
              "---\n",
              "\n",
              "*Thank you for choosing us! If you have any other questions, feel free to contact our support team.*"
            ],
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "execution_count": 22,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Execute with LCEL chain\n",
        "response = dynamic_chain.invoke(user_input)\n",
        "\n",
        "# Display the formatted FAQ (response is already a clean string)\n",
        "Markdown(response)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-S6nyqLDBpH9"
      },
      "source": [
        "With this we've limited the number of examples being given within the prompt. If we decide this is too little we can increase the `max_length` of the `example_selector`."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 23,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "pncu9b_MBpH9",
        "outputId": "c40b0a9b-ee10-4f9b-e92e-2df50d5045c7"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Create a structured Markdown FAQ with anchor links, headers, and formatting conventions for readability.\n",
            "Make sure to bold key terms and important information in the answers.\n",
            "\n",
            "**Example:**\n",
            "\n",
            "Example input:\n",
            "Q1: What is your return policy?\n",
            "A1: We accept returns within 30 days with the original receipt.\n",
            "Q2: Do you ship internationally?\n",
            "A2: Yes, we ship to over 50 countries worldwide.\n",
            "Q3: How can I track my order?\n",
            "A3: After your order is shipped, you'll receive a tracking link via email.\n",
            "\n",
            "Example Output:\n",
            "# Frequently Asked Questions\n",
            "\n",
            "\n",
            "# Frequently Asked Questions\n",
            "\n",
            "## [1. What is your return policy?](#1-what-is-your-return-policy)\n",
            "\n",
            "We accept returns within **30 days** with the original **receipt**.\n",
            "\n",
            "## [2. Do you ship internationally?](#2-do-you-ship-internationally)\n",
            "\n",
            "Yes, we ship to over **50 countries** worldwide.\n",
            "\n",
            "## [3. How can I track my order?](#3-how-can-i-track-my-order)\n",
            "\n",
            "After your order is shipped, you'll receive a **tracking link** via email.\n",
            "\n",
            "---\n",
            "\n",
            "Example input:\n",
            "Q1: What payment methods do you accept?\n",
            "A1: We accept Visa, Mastercard, PayPal, and Apple Pay.\n",
            "Q2: Can I change my shipping address after ordering?\n",
            "A2: Only if your order hasn't shipped yet. Contact support ASAP.\n",
            "Q3: Do you offer gift wrapping?\n",
            "A3: Yes! You can select gift wrapping during checkout.\n",
            "\n",
            "Example Output:\n",
            "# Frequently Asked Questions\n",
            "\n",
            "\n",
            "# Frequently Asked Questions\n",
            "\n",
            "## [1. What payment methods do you accept?](#1-what-payment-methods-do-you-accept)\n",
            "\n",
            "We accept **Visa**, **Mastercard**, **PayPal**, and **Apple Pay**.\n",
            "\n",
            "## [2. Can I change my shipping address after ordering?](#2-can-i-change-my-shipping-address-after-ordering)\n",
            "\n",
            "Only if your order hasn't shipped yet. Contact support **ASAP**.\n",
            "\n",
            "## [3. Do you offer gift wrapping?](#3-do-you-offer-gift-wrapping)\n",
            "\n",
            "Yes! You can select **gift wrapping** during checkout.\n",
            "\n",
            "---\n",
            "\n",
            "Example input:\n",
            "Q1: What are your store hours?\n",
            "A1: We're open Monday through Friday from 9am to 9pm, and weekends from 10am to 7pm.\n",
            "Q2: Do you offer price matching?\n",
            "A2: Yes, we'll match any price from authorized retailers for identical products.\n",
            "Q3: What is your warranty policy?\n",
            "A3: All electronics come with a standard 1-year manufacturer warranty.\n",
            "\n",
            "Example Output:\n",
            "# Frequently Asked Questions\n",
            "\n",
            "\n",
            "# Frequently Asked Questions\n",
            "\n",
            "## [1. What are your store hours?](#1-what-are-your-store-hours)\n",
            "\n",
            "We're open **Monday through Friday** from **9am to 9pm**, and **weekends** from **10am to 7pm**.\n",
            "\n",
            "## [2. Do you offer price matching?](#2-do-you-offer-price-matching)\n",
            "\n",
            "Yes, we'll match any price from **authorized retailers** for **identical products**.\n",
            "\n",
            "## [3. What is your warranty policy?](#3-what-is-your-warranty-policy)\n",
            "\n",
            "All electronics come with a standard **1-year manufacturer warranty**.\n",
            "\n",
            "---\n",
            "\n",
            "\n",
            "Now generate the FAQ section for this data:\n",
            "Q1: What are all the different payment methods that you accept for online purchases, including credit cards, digital wallets, bank transfers, and any special financing options that might be available for customers?\n",
            "A1: We accept a comprehensive range of payment methods to accommodate all our customers' preferences and needs. For credit cards, we accept Visa, Mastercard, American Express, and Discover. We also support digital wallet payments through PayPal, Apple Pay, Google Pay, Samsung Pay, and Amazon Pay. Additionally, we offer bank transfer options including ACH transfers, wire transfers, and direct debit for customers who prefer traditional banking methods. For larger purchases, we provide financing options through Affirm, Klarna, and our own in-house financing program with flexible payment plans ranging from 6 to 36 months. We also accept cryptocurrency payments including Bitcoin, Ethereum, and several other major cryptocurrencies for tech-savvy customers.\n",
            "Q2: Is it possible for me to modify or completely change my shipping address after I have already placed and confirmed my order, and if so, what are the specific conditions, timeframes, and procedures that I need to follow?\n",
            "A2: Yes, it is possible to modify your shipping address, but this depends entirely on the current status of your order in our fulfillment process. If your order has not yet been processed by our warehouse team and is still in 'pending' or 'confirmed' status, you can easily change the shipping address by logging into your account and accessing the order management section. However, once your order enters the 'processing' phase and our warehouse team begins preparing your items for shipment, address changes become much more complicated and may not be possible. If your order has already shipped, unfortunately we cannot redirect the package to a different address, but you can contact the shipping carrier directly to arrange for package interception or redirection services, though additional fees may apply. For the best chance of successful address modification, we strongly recommend contacting our customer support team immediately at support@company.com or calling our toll-free number.\n",
            "Q3: Do you provide gift wrapping services for the items that I purchase, and if you do, what are the different options available, what are the costs involved, and can I include personalized messages or special requests?\n",
            "A3: Absolutely! We offer comprehensive gift wrapping services to make your purchases extra special for any occasion. We have several gift wrapping options available: our standard gift wrap features elegant wrapping paper in various colors and patterns with matching ribbon and a bow for an additional $4.99 per item. Our premium gift wrap option includes luxury wrapping paper, silk ribbon, and decorative embellishments for $9.99 per item. For special occasions, we offer themed wrapping for holidays, birthdays, weddings, and baby showers at $7.99 per item. You can also add personalized gift messages up to 250 characters at no additional cost, and we'll include them on beautiful greeting cards. For an extra $2.99, you can upload custom messages or even photos to be printed on special cards. All gift-wrapped items are carefully packaged to ensure they arrive in perfect condition, and we offer discrete packaging options if you're sending gifts directly to recipients.\n",
            "\n"
          ]
        }
      ],
      "source": [
        "# Adjust max_length for more examples\n",
        "dynamic_faq_prompt.example_selector.max_length = 1000\n",
        "formatted_prompt = dynamic_faq_prompt.format(input=user_input)\n",
        "print(formatted_prompt)\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Tlv4t9XLBpH9"
      },
      "source": [
        "These are just a few examples of the prompt tooling available in LangChain. For example, there is actually an entire other set of example selectors beyond the `LengthBasedExampleSelector`. We'll cover them in detail in upcoming notebooks, or you can read about them in the [LangChain docs](https://langchain-cn.readthedocs.io/en/latest/modules/prompts/example_selectors.html)."
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "pinecone1",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.4"
    },
    "orig_nbformat": 4
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
